package cn.sinjet.carassist;

import com.mgroup.wazehelper.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.sinjet.myview.DoorOpenWarningView;
import cn.sinjet.viewmodel.IUpdateUI;
import cn.sinjet.viewmodel.OnMyDlgActionListener;
import cn.sinjet.viewmodel.ViewModel;
import cn.sinjet.viewmodel.ViewPropertyCenter;

public class MyActivity extends Activity implements IUpdateUI{
	protected ViewGroup mViewRoot;
	protected ViewPropertyCenter mUIMsgCenter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.d("ui", "MyActivity onCreate");
		
		SinjetApplication.getInstance().addActivity(this);
		mUIMsgCenter = ViewModel.getIns().getUIMsgSaveCenter();
//		 TTSController.getInstance(this).init();// 初始化语音模块
	}
	
     public void setMyContentView(int layout){
    	 try{
        	 LayoutInflater inflater = LayoutInflater.from(this);
 		     mViewRoot = (ViewGroup)inflater.inflate(layout,null);
    	 }catch(Exception e){
    		 e.printStackTrace();
    		 finish();
    	 }
 		 if(null == mViewRoot) finish();
 		  setContentView(mViewRoot);
     }
     
	   @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("ui", "MyActivity onResume");
//		if(MainModel.getIns().naviModel.getSettings().isKeepScreenOn)
//			getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//		else 
//			getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON, WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
		ViewModel.getIns().setContext(this);
		setUIViewProperty(mViewRoot);
	}

	   public void setUIViewProperty(ViewGroup vg) {
		   if(vg == null) return;
			for (int i = 0; i < vg.getChildCount(); i++) {
				View v = vg.getChildAt(i);
				if (v instanceof ViewGroup) {
					setUIViewProperty((ViewGroup) v);
				} else {
					mUIMsgCenter.restoreUIProperty(v);
				}
			}
		}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("ui", "MyActivity onDestory");
		SinjetApplication.getInstance().deleteActivity(this);
	}
	@Override
	public void resetContentView(int layout) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void finishActivity() {
		// TODO Auto-generated method stub
		finish();
	}
	@Override
	public void updateUI(Bundle data) {
		// TODO Auto-generated method stub
		int viewId = data.getInt("id",-1);
//		Log.d("ui", "sinjet ui update ui:"+viewId);
		if(viewId == -1 || mViewRoot == null)  return;
		View v = mViewRoot.findViewById(viewId);
		if(v != null)
			mUIMsgCenter.restoreUIProperty(v);
	}

	@Override
	public void showAlertDialog(String positive,String nagative,String content, OnMyDlgActionListener actionListen) {
		// TODO Auto-generated method stub
        		
	}

	DoorOpenWarningView mDoorOpenWarningView = null;
	@Override
	public void showDoorWarn(int[] doors) {
		// TODO Auto-generated method stub
		if(doors == null || doors.length !=5) return;
		if(mDoorOpenWarningView == null)
			mDoorOpenWarningView = new DoorOpenWarningView(this,R.style.CustomDialog);
		if(mDoorOpenWarningView != null){
			mDoorOpenWarningView.showOsdView(doors[0],doors[1],doors[2],doors[3],doors[4]);
		}
	}
	
	/*@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(mDoorOpenWarningView != null){
			if(mDoorOpenWarningView.isShowing()){
                 mDoorOpenWarningView.dismiss();
                 mDoorOpenWarningView
			}
		}
	}*/
	
}
